package com.jeejio.common.util

import android.view.View

abstract class OnPreventRepeatClickListener : View.OnClickListener {

    companion object {
        /**
         * Default click interval.
         */
        private const val MIN_CLICK_INTERVAL = 500L
    }

    private var lastOnClickTime: Long = 0
    private var onClickInterval: Long = 0

    constructor() : this(MIN_CLICK_INTERVAL)

    constructor(onClickInterval: Long) {
        this.onClickInterval = onClickInterval
    }

    /**
     * Author: JFangZ
     * Email: janezhang1901@163.com
     * Date:2021/4/19 14:48
     * Description:Callback method when interval of two click event greater than onClickInterval.
     */
    abstract fun onNotRepeatClick(v: View?)

    override fun onClick(v: View?) {
        val currentTime = System.currentTimeMillis()
        if (currentTime - lastOnClickTime < onClickInterval) {
            return
        }
        lastOnClickTime = currentTime
        onNotRepeatClick(v)
    }

}